﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace CPPEI.MultiDimensional
{
    public class MultiDimensionalService
    {
        /// <summary>
        /// 创建维度
        /// </summary>
        /// <param name="dimension"></param>
        /// <returns></returns>
        public bool CreateDimension(Data.Dimension dimension)
        {
            string sql = "insert into [md_dimension] (code,name) values (@code,@name)";
            SqlParameter pCode = new SqlParameter("@code", dimension.Code);
            SqlParameter pName = new SqlParameter("@name", dimension.Name);

            if (DataAccess.DB.ExecuteNonQuery(sql, new SqlParameter[] { pCode, pName }) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 创建维度
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="items">维度项集合</param>
        /// <returns></returns>
        public bool CreateDimension(Data.Dimension dimension, List<Data.DimensionItem> items)
        {
            string sql = "insert into [md_dimension] (code,name) values (@code,@name)";
            SqlParameter pCode = new SqlParameter("@code", dimension.Code);
            SqlParameter pName = new SqlParameter("@name", dimension.Name);

            if (DataAccess.DB.ExecuteNonQuery(sql, new SqlParameter[] { pCode, pName }) <= 0)
            {
                return false;
            }

            for (int index = 0; index < items.Count; index++)
            {
                string insertItemSQL = "insert into [md_dimensionitem] (code,name,level,dimension_code) values (@code,@name,@level,@dimension_code)";
                SqlParameter pItemCode = new SqlParameter("@code", items[index].Code);
                SqlParameter pItemName = new SqlParameter("@name", items[index].Name);
                SqlParameter pItemLevel = new SqlParameter("@level", items[index].Level);
                SqlParameter pDimensionCode = new SqlParameter("@dimension_code", dimension.Code);

                DataAccess.DB.ExecuteNonQuery(insertItemSQL, new SqlParameter[] { pItemCode, pItemName, pItemLevel, pDimensionCode });
            }
            return true;
        }

        /// <summary>
        /// 创建维度项
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool CreateDimensionItem(Data.Dimension dimension, Data.DimensionItem item)
        {
            string insertItemSQL = "insert into [md_dimensionitem] (code,name,level,dimension_code) values (@code,@name,@level,@dimension_code)";
            SqlParameter pItemCode = new SqlParameter("@code", item.Code);
            SqlParameter pItemName = new SqlParameter("@name", item.Name);
            SqlParameter pItemLevel = new SqlParameter("@level", item.Level);
            SqlParameter pDimensionCode = new SqlParameter("@dimension_code", dimension.Code);

            if (DataAccess.DB.ExecuteNonQuery(insertItemSQL, new SqlParameter[] { pItemCode, pItemName, pItemLevel, pDimensionCode }) > 0)
            {
                return true;
            }
            return false;
        }
    }
}
